package de.wittekind.bundesjugendspiele.client.management.model;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Pattern;

import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.RiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler.Gender;

public class CSVLoader {
	
	Scanner s;
	RiegenLoader rload;
	
	public CSVLoader( RiegenLoader r, File file) throws FileNotFoundException {
		s = new Scanner(file);
		s.useDelimiter("[,\n\r]");
		this.rload = r;
		s.nextLine();
		
	}
	
	public boolean hasNext () {
		
		return ( s.hasNextLine() );
	}
	
	public LocalSchueler nextSchueler () throws IOException {
			
			String lastname = s.next();
			
			String firstname = s.next();

			String form = s.next();

			Gender gender = parseGender(s.next());
			
			GregorianCalendar bday = parseDate(s);
			
			s.next();
			
			Discipline [] discs = 
					parseDisciplines(s);
			
			s.nextLine();
			
		LocalSchueler s = new LocalSchueler(rload, firstname, lastname, gender, bday, form, discs);
				
		return s;
	}
	
	Discipline [] parseDisciplines (Scanner s) {
		Discipline discs[] = new Discipline[4];
		for (int i = 0; i < 4; i++) {
			String str = s.next();
			if (str.equals("WURF80")) {
				discs[i] = Discipline.WURF80;
			} else if (str.equals("WURF200")) {
				discs[i] = Discipline.WURF200;
			} else if (str.equals("STOSS3000")) {
				discs[i] = Discipline.STOSS3000;
			} else if (str.equals("STOSS4000")) {
				discs[i] = Discipline.STOSS4000;
			} else if (str.equals("STOSS5000")) {
				discs[i] = Discipline.STOSS5000;
			} else if (str.equals("STOSS6250")) {
				discs[i] = Discipline.STOSS6250;
			} else if (str.equals("SPRINT50")) {
				discs[i] = Discipline.SPRINT50;
			} else if (str.equals("SPRINT75")) {
				discs[i] = Discipline.SPRINT75;
			} else if (str.equals("SPRINT100")) {
				discs[i] = Discipline.SPRINT100;
			} else if (str.equals("WEITSPRUNG")) {
				discs[i] = Discipline.WEITSPRUNG;
			} else if (str.equals("HOCHSPRUNG")) {
				discs[i] = Discipline.HOCHSPRUNG;
			} else if (str.equals("AUSDAUER800")) {
				discs[i] = Discipline.AUSDAUER800;
			} else if (str.equals("AUSDAUER1000")) {
				discs[i] = Discipline.AUSDAUER1000;
			} else if (str.equals("AUSDAUER2000")) {
				discs[i] = Discipline.AUSDAUER2000;
			} else if (str.equals("AUSDAUER3000")) {
				discs[i] = Discipline.AUSDAUER3000;
			}
			
		}
		return discs;
	}
	
	private GregorianCalendar parseDate(Scanner s) {
		
		String dStr = s.next();
		
		Scanner dateScn = new Scanner(dStr);
		dateScn.useDelimiter("[\\." + dateScn.delimiter().pattern() +"]");
		
		int d, m, y;
			
			d = dateScn.nextInt();
			m = dateScn.nextInt();
			y = dateScn.nextInt();
		
		GregorianCalendar date = new GregorianCalendar(y, m, d);
		dateScn.close();
		
		return date;
	}

	private Gender parseGender( String s ) {
		if ( s.equalsIgnoreCase("m")) return Gender.MALE;
		if ( s.equalsIgnoreCase("w") || s.equalsIgnoreCase("f") ) return Gender.FEMALE;
		return Gender.NOT_SPECIFIED;
	}


	
}
